#ifndef DBMS_SERVER_GLOBAL_DEFINITIONS_H
#define DBMS_SERVER_GLOBAL_DEFINITIONS_H

// ========================================
// DBMS Server - Global Configuration
// ========================================

#include <string>
#include <Winsock2.h>

namespace DBMS {
    namespace Server {

        // ========================================
        // �����������Ͷ���
        // ========================================
        using int8_t = signed char;
        using uint8_t = unsigned char;
        using uint_t = unsigned int;
        using uint16_t = unsigned short;
        using int16_t = short;
        using uint32_t = unsigned int;
        using int32_t = int;

        // B+��ר������
        using BPlusTreeKeyType = double;
        using BPlusTreeDataType = std::string;

        // ========================================
        // B+�����ò���
        // ========================================
        namespace BPlusTreeConfig {
            constexpr int TREE_ORDER = 20;                           // B+���Ľ���
            constexpr int MIN_KEYS = TREE_ORDER - 1;                // �ڵ���С��ֵ����
            constexpr int MAX_KEYS = 2 * TREE_ORDER - 1;            // �ڵ�����ֵ����
            constexpr int MIN_CHILDREN = MIN_KEYS + 1;               // �ڲ��ڵ���С�ӽڵ���
            constexpr int MAX_CHILDREN = MAX_KEYS + 1;               // �ڲ��ڵ�����ӽڵ���
            constexpr int MIN_LEAF_ENTRIES = MIN_KEYS;               // Ҷ�ӽڵ���С��Ŀ��
            constexpr int MAX_LEAF_ENTRIES = MAX_KEYS;               // Ҷ�ӽڵ������Ŀ��
        }

        // ========================================
        // �洢ϵͳ����
        // ========================================
        namespace StorageConfig {
            constexpr int BLOCK_SIZE_BYTES = 4096;                  // ����ҳ��С���ֽڣ�
            constexpr int MAX_BLOCKS_PER_FILE = 1048576;            // ���ļ����ҳ��
            constexpr int BUFFER_POOL_SIZE = 5;                     // ����ش�С
            constexpr float FREE_SPACE_THRESHOLD = 0.3f;            // ���пռ���ֵ
            constexpr float USED_SPACE_THRESHOLD = 0.7f;            // ���ÿռ���ֵ
        }

        // ========================================
        // ����ͨ������
        // ========================================
        namespace NetworkConfig {
            constexpr int DEFAULT_SERVER_PORT = 8080;               // Ĭ�Ϸ������˿�
            constexpr int MAX_CONCURRENT_THREADS = 10;              // ��󲢷��߳���
            constexpr int INVALID_INDEX_VALUE = -1;                 // ��Ч������ʶ
        }

        // ========================================
        // ö�����Ͷ���
        // ========================================
        enum class DatabaseBlockType {
            index,
            table,
            dictionary
        };

        enum class ComparisonOperator {
            LESS_THAN,
            LESS_EQUAL,
            EQUAL,
            GREATER_EQUAL,
            GREATER_THAN,
            NOT_EQUAL
        };

        // ========================================
        // �������ӽṹ��
        // ========================================
        struct NetworkConnection {
            SOCKET socketHandle;

            NetworkConnection() : socketHandle(INVALID_SOCKET) {}
            explicit NetworkConnection(SOCKET sock) : socketHandle(sock) {}
        };

    } // namespace Server
} // namespace DBMS

using namespace DBMS::Server;

typedef int8_t int8;
typedef uint8_t uint8;
typedef uint_t uint;
typedef uint16_t uint16;
typedef int16_t int16;
typedef uint32_t uint32;
typedef int32_t int32;
typedef BPlusTreeKeyType BPTKeyType;
typedef BPlusTreeDataType BPTDataType;

const int ORDER = BPlusTreeConfig::TREE_ORDER;
const int MINNUM_KEY = BPlusTreeConfig::MIN_KEYS;
const int MAXNUM_KEY = BPlusTreeConfig::MAX_KEYS;
const int MINNUM_CHILD = BPlusTreeConfig::MIN_CHILDREN;
const int MAXNUM_CHILD = BPlusTreeConfig::MAX_CHILDREN;
const int MINNUM_LEAF = BPlusTreeConfig::MIN_LEAF_ENTRIES;
const int MAXNUM_LEAF = BPlusTreeConfig::MAX_LEAF_ENTRIES;

#define BLOCK_SIZE StorageConfig::BLOCK_SIZE_BYTES
#define BLOCK_NUM StorageConfig::MAX_BLOCKS_PER_FILE
#define DATA_BUFFER_BLOCK_NUM StorageConfig::BUFFER_POOL_SIZE
#define PCT_FREE_THRESHOLD StorageConfig::FREE_SPACE_THRESHOLD
#define PCT_USED_THRESHOLD StorageConfig::USED_SPACE_THRESHOLD

const short server_my_port = NetworkConfig::DEFAULT_SERVER_PORT;
const int max_thread_num = NetworkConfig::MAX_CONCURRENT_THREADS;
const int INVALID_INDEX = NetworkConfig::INVALID_INDEX_VALUE;

using BlockType = DatabaseBlockType;
enum COMPARE_OPERATOR { LT, LE, EQ, BE, BT, BETWEEN };

//����������������������
#define PAGE_SIZE 4096
#define PAGES_PER_FILE 1048576  // ÿ���ļ����ӵ�е�ҳ��
#define BUFFER_POOL_CAPACITY 5 //�������ҳ������
#define HOT_PAGE_THRESHOLD 3
#define HIGH_THROUGHPUT_THRESHOLD 50
#define LOW_THROUGHPUT_THRESHOLD 30
#define MONITOR_INTERVAL 1
#define PREFETCH_THRESHOLD 60

enum class PageType { /*DATA_PAGE, INDEX_PAGE, DIRECTORY_PAGE, FREE_PAGE*/index, table, dictionary
};

struct F {
    SOCKET sockConn;
    F() : sockConn(INVALID_SOCKET) {}
    explicit F(SOCKET sock) : sockConn(sock) {}
};

#ifndef NULL
#define NULL 0
#endif

#endif // DBMS_SERVER_GLOBAL_DEFINITIONS_H
